<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新增/编辑订单</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background-color: #f5f5f5;
            margin: 0;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: bold;
        }

        input[type="text"],
        input[type="number"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }

        .button-group {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-top: 30px;
        }

        button {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button[type="submit"] {
            background-color: #4CAF50;
            color: white;
        }

        button[type="submit"]:hover {
            background-color: #45a049;
        }

        button[type="button"] {
            background-color: #f44336;
            color: white;
        }

        button[type="button"]:hover {
            background-color: #da190b;
        }

        .error {
            color: red;
            margin-top: 5px;
            font-size: 14px;
            display: none;
        }

        .loading {
            text-align: center;
            margin: 20px 0;
            display: none;
        }

        .loading:after {
            content: '';
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 2px solid #f3f3f3;
            border-top: 2px solid #3498db;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>新增/编辑订单</h1>
        <form id="orderForm" method="post" action="/orderSystem/addOrUpdate.orders">
            <input type="hidden" id="order_id" name="order_id">
            <div class="form-group">
                <label for="dish_name">菜品名称:</label>
                <input type="text" id="dish_name" name="dish_name" required>
                <div id="dish_name_error" class="error"></div>
            </div>
            <div class="form-group">
                <label for="dish_price">菜品价格:</label>
                <input type="number" id="dish_price" name="dish_price" required min="0" step="0.01">
                <div id="dish_price_error" class="error"></div>
            </div>
            <div class="button-group">
                <button type="submit">提交</button>
                <button type="button" onclick="cancelOrder()">取消</button>
            </div>
        </form>
        <div id="loading" class="loading"></div>
        <div id="error" class="error"></div>
    </div>

    <script>
        function showLoading() {
            document.getElementById('loading').style.display = 'block';
            document.getElementById('error').style.display = 'none';
        }

        function hideLoading() {
            document.getElementById('loading').style.display = 'none';
        }

        function showError(message) {
            const errorDiv = document.getElementById('error');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        function validateForm() {
            const dishName = document.getElementById('dish_name').value.trim();
            const dishPrice = document.getElementById('dish_price').value;
            let isValid = true;

            // 重置错误信息
            document.getElementById('dish_name_error').style.display = 'none';
            document.getElementById('dish_price_error').style.display = 'none';

            if (!dishName) {
                document.getElementById('dish_name_error').textContent = '请输入菜品名称';
                document.getElementById('dish_name_error').style.display = 'block';
                isValid = false;
            }

            if (!dishPrice || dishPrice <= 0) {
                document.getElementById('dish_price_error').textContent = '请输入有效的价格';
                document.getElementById('dish_price_error').style.display = 'block';
                isValid = false;
            }

            return isValid;
        }

        const urlParams = new URLSearchParams(window.location.search);
        const orderId = urlParams.get('order_id');
        console.log('获取到的orderId:', orderId);

        if (orderId) {
            showLoading();
            const xhr = new XMLHttpRequest();
            const url = `/orderSystem/queryById.orders?order_id=${orderId}`;
            console.log('请求URL:', url);
            xhr.open('GET', url, true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.onreadystatechange = function () {
                console.log('readyState:', xhr.readyState, 'status:', xhr.status);
                if (xhr.readyState === 4) {
                    hideLoading();
                    if (xhr.status === 200) {
                        try {
                            console.log('响应数据:', xhr.responseText);
                            const order = JSON.parse(xhr.responseText);
                            console.log('解析后的数据:', order);
                            document.getElementById('order_id').value = order.order_id;
                            document.getElementById('dish_name').value = order.dish_name;
                            document.getElementById('dish_price').value = order.dish_price;
                        } catch (e) {
                            console.error('解析错误:', e);
                            showError('解析数据失败：' + e.message);
                        }
                    } else {
                        console.error('请求失败:', xhr.status);
                        showError('获取订单信息失败，状态码：' + xhr.status);
                    }
                }
            };
            xhr.onerror = function() {
                hideLoading();
                console.error('网络错误');
                showError('网络请求失败，请检查网络连接');
            };
            xhr.send();
        }

        document.getElementById('orderForm').onsubmit = function(e) {
            e.preventDefault();
            console.log('表单提交开始');
            if (!validateForm()) {
                console.log('表单验证失败');
                return false;
            }
            console.log('表单验证通过，开始提交');
            console.log('菜品名称:', document.getElementById('dish_name').value);
            console.log('菜品价格:', document.getElementById('dish_price').value);
            console.log('订单ID:', document.getElementById('order_id').value);
            this.submit();
        };

        function cancelOrder() {
            window.location.href = 'toList.orders?pageNo=1&likeValue=';
        }
    </script>
</body>

</html>